home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d20
/
pulldir.arc
/
EDITSTR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-04-21
|
5KB
|
218 lines
/*
* editstr.c
* editing utility routines for general purpose use.
* copyright 1989, 1990 by Dana Bell
* This file uses the stdio.h printf(). For functions that call the
* SwiftBBS fossil routine(s) (i.e. fosprintf()), use the file
* ansibbs.c instead.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <dos.h>
extern unsigned char field;
extern ansi, trace;
int count, n, keycode, txtpos, lastpos, maxpos,insert = 1;
unsigned char edit[80];
int FALSE = 0;
int TRUE = 1;
unsigned navigate()
{
switch(keycode) {
case 75:
/* -----Left arrow */
if (txtpos)
txtpos--; /* Decrement the text pointer */
break;
case 77:
/* -----Right arrow */
if (txtpos < lastpos)
txtpos++; /* Increment the text pointer */
break;
case 72:
/* -----Up */
if (field)
field--;
return(0);
case 80:
/* -----Down */
field++;
case 73:
/* -----Page Up */
case 81:
/* -----Page Down */
return(0);
case 71:
/* -----Home */
txtpos = 0;
break;
case 79:
/* -----End */
txtpos = lastpos;
break;
case 82:
/* -----Insert key */
insert = !insert;
/* Adjust the cursor size */
break;
case 83:
/* -----Delete */
if (txtpos < lastpos) { /* Truncate the text */
memmove(&edit[txtpos], &edit[txtpos+1], strlen(&edit[txtpos+1]));
edit[maxpos-1] = 176;
lastpos--;
}
break;
default:
return(1);
/* return others */
}
return(keycode);
}
int editstr(char row, char col, const char *prompt, char *text, int maxlen)
{
/* ========================================================================
* Displays an editable entry field for prompts
* ===================================================================== */
/* unsigned char edit[80]; */
char tab[4];
char leftcol, rightcol, ch;
char *genp = "none";
strcpy(tab," "); /* convert to 3 spaces */
leftcol = col+strlen(prompt);
locate(row, col);
printf("%s",prompt);
if((genp = strchr(prompt,'\n') != NULL)) {
row++;
leftcol = col+2;
}
rightcol = leftcol+maxlen;
/* check field match */
if (strlen(text) > maxlen)
text[maxlen] = '\0';
/* creates "edit" field, prints prompt, and locates to leftcol */
for(count = 0; count < rightcol-leftcol; count++)
edit[count] = 176;
edit[count] = '\0';
memcpy(edit,text,strlen(text));
/* printf("%s",edit); */
locate (row,leftcol);
maxpos = rightcol - leftcol;
txtpos = strlen(text);
lastpos = txtpos;
/* ----- Find the cursor's size in Scan Lines */
/* -----Main loop for handling key presses */
do {
locate (row,leftcol);
printf("%s",edit);
locate (row, leftcol + txtpos);
while(!kbhit())
;
if ((keycode = getch()) != 00)
switch (keycode) {
case 3:
return(0);
/* temp control break escape */
case 0x8:
/* -----Backspace */
if (txtpos) { /* Still within the field */
if (insert) { /* truncate the string */
memmove(&edit[txtpos-1], &edit[txtpos], strlen(&edit[txtpos]));
edit[maxpos-1] = 176;
}
else /* blank the letter */
edit[txtpos-1] = ' ';
txtpos--; /* back up the text pointer */
lastpos--;
}
break;
case 0x9:
/* -----Tab */
if (txtpos +2< maxpos) {
if (insert) { /* expand the text string */
if (txtpos < maxpos - 3) {
memmove(&edit[txtpos+3],&edit[txtpos],strlen(&edit[txtpos+3]));
memcpy(&edit[txtpos],tab, 3);
printf("%s",&edit[txtpos]);
}
txtpos += 3;
lastpos += 3;
}
}
break;
case 25:
for(count = 0; count < rightcol-leftcol; count++)
edit[count] = 176;
edit[count] = '\0';
lastpos = txtpos = 0;
break;
case 13:
field++;
case 26:
fini:
/* -----Enter, Escape, or Ctrl-Z */
/* trim off fill, copy to edit, and return(); */
for(count = maxpos-1;count > -1; count--) {
if('░'==edit[count])
edit[count] = ' ';
else
break;
}
locate (row,leftcol);
printf("%s",edit);
for(count = maxpos-1;count > -1; count--) {
if(' '==edit[count])
edit[count] = '\0';
else
break;
}
strcpy(text,edit);
return(keycode);
case 27:
/* no change to original string */
return(keycode);
default:
/* -----Letter Keys */
if (txtpos < maxpos) {
if (insert) {
memmove(&edit[txtpos+1],&edit[txtpos],strlen(&edit[txtpos+1]));
edit[txtpos] = keycode;
printf("%s",&edit[txtpos]);
}
else {
edit[txtpos] = keycode;
putch(keycode);
}
txtpos++;
lastpos++;
}
} /* end of normal key switch */
else { /* get extended key */
keycode = getch();
if ((n = navigate()) == 0)
goto fini;
else if(n == 1)
return(keycode);
}
} while (1);
}